(display "\n========================================\n")

(define (smooth f)
    (define dx 0.00001)
    (lambda (x)
        (/ (+ (f (- x dx)) (f x) (f (+ x dx))) 3)))

; 引入一个随机数，返回的结果是 x + (0~1)，期望是 x + 0.5
(define (inc x) (+ x (random 2)))

; 跳跃好大，要么 1.1，要么 2.1
(display (inc 1.1))
(newline)

; 平滑后效果会好些，更接近期望 1.6
(display ((smooth inc) 1.1))
(newline)

(define (repeate f n)
    (define (compose f g)
        (lambda (x) (f (g x))))
    (define (iter i fi)
        (if (= i n)
            fi
            (iter (+ i 1)
                (compose f fi))))
    (iter 1 f))

; (display ((repeate square 2) 3))
; (newline)

; 多次平滑，效果更好，平滑次数越多越接近期望 1.6
(display (((repeate smooth 10) inc) 1.1))
(newline)

(define (smooth-n-times f n)
    (lambda (x) (((repeate smooth n) f) x)))

(display ((smooth-n-times inc 10) 1.1))
(newline)
(display "\n========================================\n")
